package inventoryServe.controller;


import inventoryServe.po.*;
import inventoryServe.service.StoreManageService;
import inventoryServe.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import inventoryServe.utils.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-06-22
 */
@Controller
public class StoreManageController {
    @Autowired
    private StoreManageService storeManageService;

    // 获取仓库管理数据
    @GetMapping("/storeManages")
    @ResponseBody
    public Object allStoreManage(HttpServletRequest req) {

        String proName = req.getParameter("proName").trim();
        String supName = req.getParameter("supName").trim();
        String useName = req.getParameter("useName").trim();
        String isIn = req.getParameter("isIn").trim();
        String isCheck = req.getParameter("isCheck").trim();

        StoreManage storeManage = new StoreManage();
        if (proName != "") {
            Product product = new Product();
            product.setName(proName);
            storeManage.setProduct(product);
        }
        if (supName != "") {
            Supplier supplier = new Supplier();
            supplier.setName(supName);
            storeManage.setSupplier(supplier);
        }
        if (useName != "") {
            User user = new User();
            user.setUsername(useName);
            storeManage.setUser(user);
        }
        if (isIn != "") {
            storeManage.setIsIn(Integer.parseInt(isIn));
        }
        if (isCheck != "") {
            storeManage.setIsCheck(Integer.parseInt(isCheck));
        }
        List<StoreManage> res = this.storeManageService.allStoreManage(storeManage);
        if (res != null) {
            return ResUtils.Success(1,"查询成功",res);
        } else {
            return ResUtils.Fail(0,"没有数据");
        }
    }


    // 增加仓库管理
    @PostMapping("storeManages/")
    @ResponseBody
    public Object addStoreManage(@RequestBody StoreManage storeManage) {

        Date now = new Date();
        storeManage.setCreateTime(now);
        storeManage.setUpdateTime(now);
        System.out.println(storeManage);
        boolean res = storeManageService.addStoreManage(storeManage);
        if (res) {
            return ResUtils.Success(1,"添加成功",storeManage);
        } else {
            return ResUtils.Fail(0,"添加失败");
        }
    }

//    check

    // 根据 仓库管理 状态 信息
    @PutMapping("/storeManages/check/{id}")
    @ResponseBody
    public Object isCheck(@PathVariable("id") Integer id,@RequestParam("isCheck") Integer isCheck,
                          @RequestParam("store") Integer storeId) {
        Date now = new Date();

        StoreManage storeManage = new StoreManage();
        storeManage.setId(id);
        storeManage.setIsCheck(isCheck);
        // 拒绝的
        if (isCheck == 2) {
            Boolean res = storeManageService.isCheck(storeManage);
            if (res) {
                return ResUtils.Success(1, "状态成功");
            } else {
                return ResUtils.Fail(0, "状态失败");
            }
        } else {
            // 入库或者出的
            // 状态成功 添加到库存中
            StoreManage storeManage1 = storeManageService.storeManageOfId(id);

            // 进库存中仓库
            Inventory inventory = new Inventory();
            Store store = new Store();
            store.setId(storeId);
            inventory.setStore(store);
            // 进库存中产品
            Product product = new Product();
            product.setId(storeManage1.getProduct().getId());
            inventory.setProduct(product);
            //进库存中数量
            inventory.setNum(storeManage1.getNum());
            inventory.setUpdateTime(now);

            if (storeManage1.getIsIn() == 0) {

                //查询是否存在 存在就修改数量
                Boolean flag;
                if (this.storeManageService.isInInventory(inventory)) {
                    System.out.println("已经存在了");
                    //执行修改操作
                    flag = this.storeManageService.editAddInventory(inventory);
                } else {
                    System.out.println("不存在新增");
                    // 不存在就新增
                    inventory.setCreateTime(now);
                    flag = this.storeManageService.addInventory(inventory);
                }
                if (flag) {
                    // 入库成功 执行 修改状态
                    Boolean res = storeManageService.isCheck(storeManage);
                    if (res) {
                        return ResUtils.Success(1, "入库成功");
                    } else {
                        return ResUtils.Fail(0, "状态失败");
                    }
                }
            } else {
                // 出库的
                //查询是否存在 不存在就直接拒绝
                Boolean flag;
                if (!this.storeManageService.isInInventory(inventory)) {
                    return ResUtils.Fail(0, "出库失败 不存在的产品");
                } else {
                    // 存在查看 数量是否满足 根据 仓库id 和产品 id 查询出库存
                    Integer inventoryNum = this.storeManageService.getInventoryNum(storeId, storeManage1.getProduct().getId());
                    if (inventoryNum > inventory.getNum()) {
                        // 库存的数量大于 出库数量 满足执行出库
                        flag = this.storeManageService.editSubInventory(inventory);
                        if (flag) {
                            // 出库成功 执行 修改状态
                            Boolean res = storeManageService.isCheck(storeManage);
                            if (res) {
                                return ResUtils.Success(1, "出库成功");
                            } else {
                                return ResUtils.Fail(0, "出库失败 服务器出错");
                            }
                        }
                    } else {
                        // 库存的数量小于 出库数量 不满足执行出库 返回
                        return ResUtils.Fail(0, "出库失败 库存数量不足");
                    }
                }
            }
        }
        return ResUtils.Fail(0, "失败 服务器出错");

    }

//    deleteStoreManage
    // 删除用户
    @DeleteMapping("storeManages/{id}")
    @ResponseBody
    public Object deleteStoreManage(@PathVariable("id") Integer id) {
        boolean res = storeManageService.deleteStoreManage(id);
        if (res) {
            return ResUtils.Success(1,"删除成功");
        } else {
            return ResUtils.Fail(0,"删除失败");
        }
    }

    // 统计 订单
    @PostMapping("storeManages/chart")
    @ResponseBody
    public Object storeManageChart(@RequestBody StoreManage storeManage) {
        System.out.println("接收到请求");
        System.out.println(storeManage);
        List<StoreManage> res = storeManageService.storeManageChart(storeManage);
        System.out.println(res);
        if (res != null) {
            return ResUtils.Success(1,"查询成功",res);
        } else {
            return ResUtils.Fail(0,"查询失败");
        }
    }
}

